package com.molinari.coding.dati.domain.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Observable;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.molinari.androidstructure.data.db.IDao;
import com.molinari.androidstructure.data.db.IOggettoEntita;
import com.molinari.coding.ControllerCoding;
import com.molinari.coding.R;
import com.molinari.coding.dati.domain.Cella;
import com.molinari.coding.dati.domain.ICella;
import com.molinari.jdbcandroid.wrap.ConnectionAndroid;

public class WrapCella extends Observable implements IDao, ICella {

	private Cella cella;
	private Context context;
	
	public WrapCella(Context context) {
		cella = new Cella();
		this.context = context;
	}

	public Object selectByIdUtente(final int idUtente) {
		final Connection cn = ControllerCoding.getConnection(context);

		final Vector<Object> celle = new Vector<Object>();
		try {
			ConnectionAndroid cnAndroid = (ConnectionAndroid) cn;
			String[] idUtenteString = new String[] { Integer.toString(idUtente) };
			String orderBy = Cella.CHIAVE + " desc";

			String selection = Cella.IDUTENTE + " = ?";

			Cursor cursor = cnAndroid.getDb().query(Cella.NOME_TABELLA, null, selection, idUtenteString, null, null, orderBy);
			if (cursor.moveToFirst()) {
				do {
					Cella cella = new Cella();
					setCellaFromCursor(cella, cursor);
					celle.add(cella);
				} while (cursor.moveToNext());
			}

			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
			cnAndroid.close();
		} catch (Exception e) {
			Log.e(e.getClass().getName(), Log.getStackTraceString(e));
		}
		return celle != null ? celle.iterator() : null;
	}
	
	@Override
	public Object selectById(final int id) {
		final Connection cn = ControllerCoding.getConnection(context);

		Cella cella = null;
		try {
			ConnectionAndroid cnAndroid = (ConnectionAndroid) cn;
			String[] idCellaString = new String[] { Integer.toString(id) };
			String orderBy = Cella.CHIAVE + " desc";

			String selection = Cella.ID + " = ?";

			Cursor cursor = cnAndroid.getDb().query(Cella.NOME_TABELLA, null, selection, idCellaString, null, null, orderBy);
			if (cursor.moveToFirst()) {
				do {
					cella = new Cella();
					setCellaFromCursor(cella, cursor);
				} while (cursor.moveToNext());
			}

			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
			cnAndroid.close();
		} catch (Exception e) {
			Log.e(e.getClass().getName(), Log.getStackTraceString(e));
		}
		return cella;
	}

	private void setCellaFromCursor(final Cella cella, final Cursor rs) {
		cella.setIdCella(rs.getInt(0));
		cella.setChiave(rs.getString(1));
		cella.setUno(rs.getString(2));
		cella.setDue(rs.getString(3));
		cella.setTre(rs.getString(4));
		cella.setIdUtente(rs.getInt(5));
	}

	@Override
	public Iterator<Object> selectWhere(final String where) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterator<Object> selectAll() {
		final Vector<Object> celle = new Vector<Object>();
		final Connection cn = ControllerCoding.getConnection(context);
		final ConnectionAndroid cnAndroid = (ConnectionAndroid) cn;
		try {
			String orderBy = Cella.CHIAVE + " desc";
			Cursor cursor = cnAndroid.getDb().query(Cella.NOME_TABELLA, null, null, null, null, null, orderBy);
			if (cursor.moveToFirst()) {
				do {
					Cella cella = new Cella();
					setCellaFromCursor(cella, cursor);
					celle.add(cella);
				} while (cursor.moveToNext());
			}

			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
			cnAndroid.close();
		} catch (Exception e) {
			
		}

		return celle != null ? celle.iterator() : null;
	}

	@Override
	public boolean insert(final Object oggettoEntita) {
		final ConnectionAndroid cnAndroid = (ConnectionAndroid) ControllerCoding.getConnection(context);;
		final SQLiteDatabase db = cnAndroid.getDb();

		final Cella cella = (Cella) oggettoEntita;

		ContentValues values = new ContentValues();
		values.put(Cella.CHIAVE, cella.getChiave());
		values.put(Cella.UNO, cella.getUno());
		values.put(Cella.DUE, cella.getDue());
		values.put(Cella.TRE, cella.getTre());
		values.put(Cella.IDUTENTE, cella.getIdUtente());
		if (db.insert(Cella.NOME_TABELLA, null, values) == -1) {
			return false;
		}

		try {
			cnAndroid.close();
		} catch (SQLException e) {
			Log.e("Errore nell'inserimento della cella", Log.getStackTraceString(e));
		}
		return true;

	}

	@Override
	public boolean delete(final int id) {

		ConnectionAndroid cnAndroid = (ConnectionAndroid) ControllerCoding.getConnection(context);;
		final SQLiteDatabase db = cnAndroid.getDb();
		String[] whereArgs = new String[] { Integer.toString(id) };
		if (db.delete(Cella.NOME_TABELLA, Cella.ID + " = ?", whereArgs) == -1) {
			return false;
		}

		try {
			ControllerCoding.getSingleton().getConnectionPool(context).chiudiOggettiDb(cnAndroid);
		} catch (android.database.SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		} catch (SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		}

		return true;
	}

	@Override
	public boolean update(final Object oggettoEntita) {
		ConnectionAndroid cnAndroid = (ConnectionAndroid) ControllerCoding.getConnection(context);;

		final SQLiteDatabase db = cnAndroid.getDb();
		Cella cella = (Cella) oggettoEntita;

		final ContentValues values = new ContentValues();
		values.put(Cella.CHIAVE, cella.getChiave());
		values.put(Cella.UNO, cella.getUno());
		values.put(Cella.DUE, cella.getDue());
		values.put(Cella.TRE, cella.getTre());
		values.put(Cella.IDUTENTE, cella.getIdUtente());
		
		String[] whereArgs = new String[] { Integer.toString(cella.getIdUtente()), cella.getChiave() };

		if (db.update(Cella.NOME_TABELLA, values, Cella.IDUTENTE + " = ? and " + Cella.CHIAVE + " = ?", whereArgs) == -1) {
			return false;
		}

		try {
			ControllerCoding.getSingleton().getConnectionPool(context).chiudiOggettiDb(cnAndroid);
		} catch (android.database.SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		} catch (SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		}

		return true;
	}

	@Override
	public boolean deleteAll() {
		ConnectionAndroid cnAndroid = (ConnectionAndroid) ControllerCoding.getConnection(context);;
		final SQLiteDatabase db = cnAndroid.getDb();
		if (db.delete(Cella.NOME_TABELLA, "1", null) == -1) {
			return false;
		}
		try {
			ControllerCoding.getSingleton().getConnectionPool(context).chiudiOggettiDb(cnAndroid);
		} catch (android.database.SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		} catch (SQLException e) {
			Log.e(WrapCella.class.getName(), Log.getStackTraceString(e));
		}

		return true;
	}

	@Override
	public IOggettoEntita getEntitaPadre() {
		return cella;
	}

	@Override
	public void notifyObservers() {
		super.notifyObservers();
	}

	@Override
	protected synchronized void setChanged() {
		super.setChanged();
	}

	@Override
	public IOggettoEntita getEntita() {
		return getEntitaPadre();
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	@Override
	public int getIdCella() {
		return cella.getIdCella();
	}

	@Override
	public void setIdCella(int idCella) {
		cella.setIdCella(idCella);
	}

	@Override
	public String getChiave() {
		return cella.getChiave();
	}

	@Override
	public void setChiave(String chiave) {
		cella.setChiave(chiave);
	}

	@Override
	public String getDue() {
		return cella.getDue();
	}

	@Override
	public void setDue(String due) {
		cella.setDue(due);
	}

	@Override
	public String getTre() {
		return cella.getTre();
	}

	@Override
	public void setTre(String tre) {
		cella.setTre(tre);
	}

	@Override
	public int getIdUtente() {
		return cella.getIdUtente();
	}

	@Override
	public void setIdUtente(int idUtente) {
		cella.setIdUtente(idUtente);
	}

	@Override
	public String getUno() {
		return cella.getUno();
	}

	@Override
	public void setUno(String uno) {
		cella.setUno(uno);
	}

}
